package com.marsyang.display
{
	import com.marsyang.display.ui.SuperButton;
	import com.marsyang.events.EffectEvent;
	import com.marsyang.model.ModelLocator;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	public class MenuBar extends Sprite
	{
		private var params:Object;
		private var btnWidth:Number = 100;
		private var btnHeight:Number = 20;
		private var effectEvent:EffectEvent;
		private var menuLabel:Array = ["CONTENT 1", "CONTENT 2", "CONTENT 3", "CONTENT 4"];
		
		public var model:ModelLocator = ModelLocator.getInstance();
		
		public function MenuBar()
		{
			params = {_alpha:0.2, _width:btnWidth, _height:btnHeight, _colorOver:0xffffff, _textColor:0x666666, _textColorOver:0xff0000};
			createHome();
			createMenu();			
		}
		private function createHome():void
		{
			var btnHome:SuperButton = new SuperButton(params);
			btnHome.x = 0;
			btnHome.y = 110;
			btnHome._label = "HOME";
			btnHome.create();
			addChild(btnHome);
			btnHome.addEventListener(MouseEvent.CLICK, homeClickHandler);
		}
		private function createMenu():void
		{
			for(var i:uint=0;i<menuLabel.length;i++){
				var m:SuperButton = new SuperButton(params);
				m.name = "m"+i;
				m.y = 30*i+140;
				m._label = menuLabel[i];
				m.create();
				addChild(m);
				m.addEventListener(MouseEvent.CLICK, menuClickHandler);
			}
		}
		private function homeClickHandler(e:MouseEvent):void
		{
			model.previousMenu = model.currentMenu;
			model.currentMenu = 0;
			if(model.previousMenu == model.currentMenu) return;
			effectEvent = new EffectEvent(EffectEvent.GO_HOME);
			dispatchEvent(effectEvent);
		}
		private function menuClickHandler(e:MouseEvent):void
		{
			var n:uint = Math.floor(Number(String(e.currentTarget.name).split("m")[1]))+1;
			model.previousMenu = model.currentMenu;
			model.currentMenu = n;
			if(model.previousMenu == model.currentMenu) return;
			effectEvent = new EffectEvent(EffectEvent.GO_CONTENT);
			dispatchEvent(effectEvent);
		}
	}
}